﻿@using Nop.Services.Stores
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.Configuration.Settings.AllSettings").Text;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-configuration.png")" alt="" />
        @T("Admin.Configuration.Settings.AllSettings")
    </div>
</div>
<table class="adminContent">
    <tr>
        <td>
            @T("Admin.Configuration.Settings.AllSettings.Description")
        </td>
    </tr>
    <tr>
        <td>
            <div id="settings-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#settings-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("AllSettings", "Setting"))",
                                    type: "POST",
                                    dataType: "json",
                                    contentType: "application/json"
                                },
                                create: {
                                    url: "@Html.Raw(Url.Action("SettingAdd", "Setting"))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                update: {
                                    url:"@Html.Raw(Url.Action("SettingUpdate", "Setting"))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                destroy: {
                                    url: "@Html.Raw(Url.Action("SettingDelete", "Setting"))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                parameterMap: function(data, operation) {
                                    if (operation != "read") {
                                        return data;
                                    } else {
                                        //for some reasons only such "Filter" data be parsed
                                        return JSON.stringify(data);
                                    }
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors",
                                model: {
                                    id: "Id",
                                    fields: {
                                        Name: { editable: true, type: "string" },
                                        Value: { editable: true, type: "string" },
                                        Store: { editable: true, type: "string" },
                                        StoreId: { editable: true, type: "number" },
                                        Id: { editable: false, type: "number" }
                                    }
                                }
                            },
                            requestEnd: function (e) {
                                if (e.type == "create" || e.type == "update") {
                                    this.read();
                                }
                            },
                            error: function (e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        sortable: true,
                        toolbar: ["create"],
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        filterable: {
                            extra: false,
                            operators: {
                                string: {
                                    contains: "Contains",
                                    eq: "Is equal to",
                                    startswith: "Starts with"
                                }
                            }
                        },
                        scrollable: false,
                        columns: [{
                            field: "Name",
                            title: "@T("Admin.Configuration.Settings.AllSettings.Fields.Name")",
                            width: 300,
                            filterable: true,
                            sortable: true
                        }, {
                            field: "Value",
                            title: "@T("Admin.Configuration.Settings.AllSettings.Fields.Value")",
                            width: 300,
                            filterable: true,
                            sortable: true
                        }, {
                            field: "StoreId",
                            title: "@T("Admin.Configuration.Settings.AllSettings.Fields.StoreName")",
                            width: 150,
                            editor: storeDropDownEditor, 
                            template: "#:Store#",
                            filterable: false,
                            sortable: false
                        }, {
                            command: [{
                                name: "edit",
                                text: "@T("Admin.Common.Edit")"
                            }, {
                                name: "destroy",
                                text: "@T("Admin.Common.Delete")"
                            }],
                            width: 200,
                            filterable: false
                        }]
                    });
                });
                
                //local datasource
                var allStores = [
                    {
                        Id : 0,
                        Name : "@T("Admin.Configuration.Settings.AllSettings.Fields.StoreName.AllStores")"
                    }
                    @{
                        var storeService = EngineContext.Current.Resolve<IStoreService>();
                        var allStores = storeService.GetAllStores();
                    }
                    @foreach (var store in allStores)
                    {
                        <text>
                        , {
                            Id: @(store.Id),
                            Name: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(store.Name)))"
                        }
                        </text>
                    }
                ];

                function storeDropDownEditor(container, options) {
                    $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:StoreId"/>')
                        .appendTo(container)
                        .kendoDropDownList({
                            autoBind: false,
                            dataSource: allStores
                        });
                }
            </script>
        </td>
    </tr>
</table>
